const { getCardByUserList } = require("../../../service/carInfo");
const { getUserById } = require("../../../service/user");
const { WebSocketConnect, resultUtil } = require("../../webSocketUtil");

function index() { }
index.prototype.onOpen = async function () {
    let list = await getCardByUserList(26)
    this.success(list)
}
index.prototype.onMessage = function (data) {
    try {
        let message = JSON.parse(data)
        let paramsData = message.data || {}
        switch (message.event) {
            case 'send':
                if (!message.uid) {
                    WebSocketConnect.sendTo(resultUtil.responseErr(message.requestId, 'error', "用户id不存在"), this.uid)
                    return
                }
                getUserById(message.uid).then(user => {
                    if (!user) {
                        WebSocketConnect.sendTo(resultUtil.responseErr(message.requestId, 'error', "用户不存在"), this.uid)
                        return
                    }
                    WebSocketConnect.sendTo(resultUtil.responseOk(message.requestId, paramsData.event, paramsData.data), user.id)
                    WebSocketConnect.sendTo(resultUtil.responseReply(message.requestId), this.uid)
                })
                break;

            case 'all':
                WebSocketConnect.sendAll(resultUtil.responseOk(message.requestId, paramsData.event, paramsData), [this.uid])
                break;
        }
    } catch (error) {
        console.error('admin.index.onMessage', error)
    }

}
module.exports = index